package com.xaicode.auth.base;

import com.xaicode.auth.domain.params.PageVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;

/**
 * 基础 service-mybatis 接口实现
 * 
 * @date 2024/12/6
 */
public class BaseMpServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseMpService<T> {

    /**
     * 分页参数从1开始
     *
     * @see #startPage(PageVo)
     */
    @Deprecated
    protected void getPage(Pageable pageable) {
        getPage(pageable, true);
    }

    /**
     * 分页参数从1开始
     *
     * @see #startPage(PageVo)
     */
    @Deprecated
    protected void getPage(Pageable pageable, boolean isOrder) {
        if (isOrder) {
            String order = null;
            if (pageable.getSort() != null) {
                order = pageable.getSort().toString();
                order = order.replace(":", " ");
                if ("UNSORTED".equals(order)) {
                    PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
                    return;
                }
            }
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize(), order);
        } else {
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
        }
    }


    /**
     * 启动分页
     *
     * @param pageVo .
     */
    protected void startPage(PageVo pageVo) {
        PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
    }

    /**
     * 终止分页
     */
    protected void clearPage() {
        PageHelper.clearPage();
    }

}
